package com.systop.dxjj.modules.orders.service.impl;

import com.alibaba.fastjson.JSON;
import com.systop.dxjj.common.pojo.Orders;
import com.systop.dxjj.common.pojo.OrdersVO;
import com.systop.dxjj.common.pojo.StudentUser;
import com.systop.dxjj.common.pojo.TeacherUser;
import com.systop.dxjj.common.utils.JwtUtil;
import com.systop.dxjj.common.utils.R;
import com.systop.dxjj.common.utils.RedisCache;
import com.systop.dxjj.modules.orders.mapper.OrdersMapper;
import com.systop.dxjj.modules.orders.service.OrdersService;
import com.systop.dxjj.modules.recruitinfo.service.RecruitinfoService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    private RedisCache redisCache;
    StudentUser studentUser;
    TeacherUser teacherUser;
    @Autowired
    RecruitinfoService recruitinfoService;

    //根据id查询订单
    public R getById(Integer orderid) {
        OrdersVO ordersById = ordersMapper.getOrdersById(orderid);
        return R.ok().put("orders",ordersById);
    }

    //查询全部订单
    public R getAll(){
        List<OrdersVO> allOrders = ordersMapper.getAllOrders();
        return R.ok().put("allOrders",allOrders);

    }

    //删除
    public R deleteById(Integer orderid){
        int i = ordersMapper.deleteByOrderid(orderid);
        if (i>0) {
            return R.ok().put("msg", "删除成功");
        }else{
            return R.ok().put("msg", "删除失败");
        }
    }

    //修改状态
    @Override
    public Integer updateOrderStatue(Integer orderid) {
        OrdersVO ordersById = ordersMapper.getOrdersById(orderid);
        Integer statue1 = ordersById.getStatue();
        Integer orderid1 = ordersById.getOrderid();
        if (statue1 == 1){
            ordersMapper.updateOrderStatue(orderid1, 0);
            return 1;
        }else if (statue1 == 0){
            ordersMapper.updateOrderStatue(orderid1,1);
            return 1;
        }else {
            return 0;
        }
    }

    //查询教师本人订单
    @Override
    public R getByTeachId(HttpServletRequest request) {
        String token = request.getHeader("token");
        String userid;
        try {
            Claims claims = JwtUtil.parseJWT(token);
            userid = claims.getSubject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("token非法");
        }
        String tearedisKey = "login:" + userid;
        Object cacheObject = redisCache.getCacheObject(tearedisKey);
        if (cacheObject instanceof StudentUser) {
            teacherUser = (TeacherUser) cacheObject;
        } else {
            teacherUser = JSON.parseObject(JSON.toJSON(cacheObject).toString(), TeacherUser.class);
        }
        Integer teachid1 = teacherUser.getTeacher().getTeachid();
        List<OrdersVO> ordersByTeachId = ordersMapper.getOrdersByTeachId(teachid1);
        return R.ok().put("ordersByTeachId",ordersByTeachId);
    }

    //添加订单
    @Override
    public R addOrders(@PathVariable Integer recruitid, HttpServletRequest request) {
        if (recruitid == null){
            return R.ok().put("start",0);
        }else {
            Orders orders = new Orders();
            orders.setRecruitid(recruitid);
            //获取当前登录的学生
            String token = request.getHeader("token");
            String stuid;
            try {
                Claims claims = JwtUtil.parseJWT(token);
                stuid = claims.getSubject();
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("token非法");
            }
            String sturedisKey = "stulogin:" + stuid;
            Object cacheObject = redisCache.getCacheObject(sturedisKey);
            if (cacheObject instanceof StudentUser) {
                studentUser = (StudentUser) cacheObject;
            } else {
                studentUser = JSON.parseObject(JSON.toJSON(cacheObject).toString(), StudentUser.class);
            }
            Integer studentid = studentUser.getStudent().getStudentid();
            orders.setStudentid(studentid);
            //获取teachid
            Integer teachidByRecruitid = recruitinfoService.getTeachidByRecruitid(recruitid);
            orders.setTeachid(teachidByRecruitid);
            orders.setStatue(0);
            int i = ordersMapper.addOrders(orders);
            if (i != 0){
                return R.ok().put("start",1);
            }else {
                return R.ok().put("start",0);
            }
        }
    }
}
